<template>
  <div class="indexHome">
    <article class="slideshow">
      <swiper
        style="height:180px"
        indicator-dots="true"
        autoplay="true"
        interval="5000"
        duration="1000"
      >
        <block v-for="(item, index) in slideimg" :index="index" :key="index">
          <swiper-item>
            <img :src="item.url" mode="aspectFit" />
          </swiper-item>
        </block>
      </swiper>
    </article>
    <div class="homeContent">
      <ul>
        <li @click="linkTo(index)" v-for="(item,index) in homeData" :index="index" :key="index">
          <img :src="item.imgurl" alt />
          <p>{{item.name}}</p>
        </li>
      </ul>
    </div>
    <button @click="Getinfo" lang="zh_CN" v-if="currentType" open-type="getUserInfo">获取用户信息</button>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      slideimg: [
        {
          url:
            "http://r.photo.store.qq.com/psc?/V12xBNEP0wyzrG/S5z.*ov051w3o7gBaJERRo7HF5ht7cqHDk9vnC4fTPAayCUjimbm7ACbJo89i1pcgLQ.TtVy9KX1wQPly1idSzzGFMzh*dePq0XPp9T5Jko!/r"
        },
        {
          url:
            "http://r.photo.store.qq.com/psc?/V12xBNEP0wyzrG/S5z.*ov051w3o7gBaJERRqTEKmH5QALE61HroY8ZJ60gPCiQya4zJ10YRTDpGv3ICC6U5DuczFiSmYQ9We9PK2yZhHSmGsG0NRVD9nn*swg!/r"
        }
      ],
      homeData: [
        {
          imgurl:
            "http://r.photo.store.qq.com/psc?/V12xBNEP0wyzrG/S5z.*ov051w3o7gBaJERRtuWfTYME3reLV*fYfa35E535u7tUS2hC8DFQxfHMrqv8odNAdQPp*tzEF3c.kwkjytCB9kmK00CgIR26E0cmh4!/r",
          name: "活动"
        },
        {
          imgurl:
            "http://r.photo.store.qq.com/psc?/V12xBNEP0wyzrG/S5z.*ov051w3o7gBaJERRtuWfTYME3reLV*fYfa35E535u7tUS2hC8DFQxfHMrqv8odNAdQPp*tzEF3c.kwkjytCB9kmK00CgIR26E0cmh4!/r",
          name: "问卷"
        },
        {
          imgurl:
            "http://r.photo.store.qq.com/psc?/V12xBNEP0wyzrG/S5z.*ov051w3o7gBaJERRtuWfTYME3reLV*fYfa35E535u7tUS2hC8DFQxfHMrqv8odNAdQPp*tzEF3c.kwkjytCB9kmK00CgIR26E0cmh4!/r",
          name: "夏令营"
        },
        {
          imgurl:
            "http://r.photo.store.qq.com/psc?/V12xBNEP0wyzrG/S5z.*ov051w3o7gBaJERRtuWfTYME3reLV*fYfa35E535u7tUS2hC8DFQxfHMrqv8odNAdQPp*tzEF3c.kwkjytCB9kmK00CgIR26E0cmh4!/r",
          name: "课程"
        }
      ],
      linkurl: [
        {
          url: "../event/main"
        },
        {
          url: "../question/questionStart/main"
        },
        {
          url: "../event/main"
        },
        {
          url: "../event/main"
        }
      ],
      currenturl: "",
      currentType: true
    };
  },

  components: {},

  methods: {
    linkTo(index) {
      this.currenturl = this.linkurl[index].url;
      wx.redirectTo({ url: this.currenturl });
    },
    Getinfo: function(e) {
      console.log("e", e);
    },
    hasid() {
      let that = this;
      wx.getSetting({
        success(res) {
          if (res.authSetting["scope.userInfo"]) {
            that.currentType = false;
            wx.getUserInfo({
              success(res) {
                // console.log("信息：", res);
                wx.login({
                  success(res) {
                    var code = res.code;
                    Vue.prototype.openid = res.code;
                    // console.log("登录：", res);
                    if (code) {
                      that.$fly
                        .post("/getAppid", {
                          code: res.code
                        })
                        .then(function(res) {
                          console.log("成功", res);
                          wx.setStorageSync({
                            key: "openid",
                            data: res.data.data.openid
                          });
                        })
                        .catch(function(err) {
                          console.log("失败", err);
                        });
                    } else {
                      console.log("获取用户登录态失败！");
                    }
                  },
                  fail: function(error) {
                    console.log("login failed " + error);
                  }
                });
              }
            });
          } else {
            that.currentType = true;
          }
        }
      });
    }
  },
  onReady() {},

  created() {
    // let app = getApp()
    this.hasid();
  },
  onLoad() {}
};
</script>

<style scoped>
.slideshow img {
  height: 100%;
  width: 100%;
}
.homeContent {
  margin-top: 20px;
  margin: 5px;
}
.homeContent ul {
  display: flex;
  flex-wrap: wrap;
}
.homeContent ul li {
  width: 44%;
  height: 350rpx;
  background-color: #f7f9f9;
  margin: 5px 10px;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  font-size: 16px;
}
.homeContent ul li img {
  width: 90%;
  height: 80%;
}
</style>

